<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    body {
      background-color: #333;
    }
    .box {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 100px;
      height: 100px;
      background-color: skyblue;
      border-radius: 50%;
    }
    button {
      float: left;
      margin-top: 500px;
      padding: 20px;
      background-color: yellow;
    }
  </style>
  <body>
    <div class="box"></div>
    <button style="margin-left: 800px">向上</button>
    <button>向下</button>
    <button>向左</button>
    <button>向右</button>
    <button>停止</button>
    <script>
      var box = document.querySelector(".box");
      var btn = document.getElementsByTagName("button");
      var leader;
      var leader1;
      var timer1;
      var timer2;
      var timer3;
      var timer4;

      btn[0].onmousedown = function () {
        timer1 = setInterval(function () {
          leader = box.offsetTop;
          box.style.top = leader - 5 + "px";
        }, 1000 / 60);
      };
      btn[1].onmousedown = function () {
        timer2 = setInterval(function () {
          leader = box.offsetTop;
          box.style.top = leader + 5 + "px";
        }, 1000 / 60);
      };
      btn[2].onmousedown = function () {
        timer3 = setInterval(function () {
          leader1 = box.offsetLeft;
          box.style.left = leader1 - 5 + "px";
        }, 1000 / 60);
      };
      btn[3].onmousedown = function () {
        timer4 = setInterval(function () {
          leader1 = box.offsetLeft;
          box.style.left = leader1 + 5 + "px";
        }, 1000 / 60);
      };

      btn[4].onclick = function () {
        clearInterval(timer1);
        clearInterval(timer2);
        clearInterval(timer3);
        clearInterval(timer4);
      };
    </script>
  </body>
</html>
